<?php
namespace app\api\Model;
use think\View;
use think\Controller;
use think\Model;
use \app\data\Data;
use think\Request;
use app\api\model\Client;
use think\Db;

class Common extends Model{
    /*返回分页--起始页*/
    public function page_data($count,$pages,$number){ 
        if($count==0){
            $end_row = 0;
            $start_row = 0;
        }else{
            $end_row = $pages * $number;
            $start_row = $end_row - $number + 1;
            $end_row = $end_row > $count ? $count : $end_row;      
        }
        $row['end'] = $end_row;
        $row['start'] = $start_row;
        return $row;
    }

    public function msg($status,$info){
        return [
            "status" => $status,
            "info" => $info
        ];
    }
        
    /*
     * @知果汇
     * @获取缓存
     * @2017-09-12
     * @潘龙兵
     */
    public function _zgh_user_info(){
      $zgh_user_info = session('zgh_user_info') ? session('zgh_user_info'):cookie('zgh_user_info');
      $zgh_user_info = json_decode($zgh_user_info,true);
      return $zgh_user_info;
    }
	public function _zgh_admin_info(){
        $zgh_user_info = session('userinfo') ? session('userinfo'):cookie('userinfo');
        $zgh_user_info = json_decode($zgh_user_info,true);
        return $zgh_user_info;
    }

	/*
     * @知果汇
     * @生成密码
     * @2017-09-12
     * @潘龙兵
     * @$pwd ： 明文密码
        $rand : 四位数的随机数
        $data['password'] : 新生成的密码
     */
    public function pwd($pwd=123456) { 
        $rand = $this -> GetRandStr();
        $data['rand'] = $rand;
        $data['password'] = MD5($rand.MD5($pwd));
        return json_encode($data);
    }


    /*
     * @知果汇
     * @验证密码
     * @2016-11-14
     * @潘龙兵
     * @$pwd ： 明文密码 
     */
    public function change_pwd($pwd,$rand) {
        $password = MD5($rand.MD5($pwd));
        return $password;
    }

    /*
     * @知果汇
     * @生成随机数
     * @2017-09-12
     * @潘龙兵
     * @$len: 随机数长度 默认4位
	 * @$chars: 随机数数组
	 * @$charsLen: 随机数数组长度
	 * @$output: 输出数据
     */
	public function GetRandStr($len=4) {
	    $chars = array(
	        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
	        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
	        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
	        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
	        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
	        "3", "4", "5", "6", "7", "8", "9"
	    );
	    $charsLen = count($chars) - 1;
	    shuffle($chars);
	    $output = "";
	    for ($i=0; $i<$len; $i++){
	        $output .= $chars[mt_rand(0, $charsLen)];
	    }
	    return $output;
	}
    
 
    public function tree($a,$pid=0){  

        $tree = array();                                //每次都声明一个新数组用来放子元素  
        foreach($a as $v){  
            if($v['pid'] == $pid){                      //匹配子记录  
                $v['children'] = $this->tree($a,$v['a_id']); //递归获取子记录  
                $tree[] = $v;                           //将记录存入新数组  
            }  
        }  
        return $tree;                                  //返回新数组  
    }    

    /*
    * 管理员日志信息获取
    */
    public function admin_log(){
        $user_info = $this->_zgh_user_info();
        $info['uid'] = $user_info['id'];
        $zgh_user = new data('zgh_user');
        $field = 'email,phone';
        $res = $zgh_user->_find($info,$field);
        $info['email'] = $res['email'];
        $info['phone'] = $res['phone'];
        $info['username'] = $user_info['username'];
        $info['all_url'] = Request::instance()->url(true);
        $info['url'] = Request::instance()->baseurl(true);
        $info['time'] = time();
        $Client = new Client;
        $form = $Client -> get_ip_from();
        $info['ip'] = $form['ip'];
        // dump($info);exit;
        return $info;
    }

    /*
    * 存储管理员日志
    */
    public function save_admin_log($controller,$value,$type){
        // echo 1;exit;
        $result = $this -> admin_log();
        $result['controller'] = $controller;
        $result['value'] = $value;
        $result['type'] = $type;
        $info  = $this->admin_log_add($result);
        // dump($info);exit;
        return $info;
    }


    public function admin_log_add($info){
        $add = Db::connect("db_mongo")->name("AdminLog")->insert($info);
        // var_dump($add);exit;
        if($add){
            return "success";
        }else{
            return "error";
        }
    }

   
}